#!/bin/bash

REPS=2
SIZE=100
CPUS=4
SED_P='real \([0-9]*\.[0-9]*\)'
BINS=(o c)
BIN_STRINGS=(openmp cilk)
SEQ=0

# CSV Format: Program, CPUs, Size, Time

function run_seq {
	TIMES=$(eval "./run -r $REPS -l $SIZE -s" | sed -n "s/$SED_P/\1/p")
	if [ ${PIPESTATUS[0]} -eq 0 ]
	then
		for time in $TIMES
		do
			echo "sequential,$CPUS,$SIZE,$time"
		done
	else
		echo "ERROR sequential returned not 0" 1>&2
	fi
}

function run {
	for (( b=0; b < 2; b++ ))
	do
		TIMES=$(eval "./run -r $REPS -l $SIZE -p $CPUS -${BINS[b]}" | sed -n "s/$SED_P/\1/p")
		if [ ${PIPESTATUS[0]} -eq 0 ]
		then
			for time in $TIMES
			do
				echo "${BIN_STRINGS[b]},$CPUS,$SIZE,$time"
			done
		else
			echo "ERROR ${BIN_STRINGS[b]} returned not 0" 1>&2
		fi
	done
}

while [ "$#" -gt 0 ]
do
	case "$1" in
	-h|--help)
		usage
		exit 0
		;;
	-q|--seq)
		SEQ=1
		;;
	-r|--repetitions)
		REPS="$2"
		shift
		;;
	-c|--cpus)
		CPUS="$2"
		shift
		;;
	-s|--size)
		SIZE="$2"
		shift
		;;
	--)
		break
		;;
	-*)
		echo "Invalid option '$1'. Use --help to see the valid options" >&2
		exit 1
		;;
	# an option argument, continue
	*)	;;
	esac
	shift
done

if [ $SEQ -eq 1 ]
then
	run_seq
else
	run
fi
